<?php
/**
 * This file is part of the Jindai.
 * @copyright Copyright (c) 2019 All Rights Reserved.
 * @author jindai <jindai0305@gmail.com>
 */

namespace App\Http\Controllers\Api\v1;


use App\Http\Resources\Song;
use App\Http\Resources\SongMenu;
use App\Http\Resources\SongMenuCollection;
use App\Repositories\MusicRepository;
use App\Repositories\SongMenuRepository;
use App\Repositories\SongRepository;
use Illuminate\Http\Request;

/**
 * Class MusicController
 * @package App\Http\Controllers\Api\v1
 *
 * @SWG\Tag(name="Music", description="歌曲")
 */
class MusicController extends Controller
{
    /**
     * @inheritdoc
     */
    public function __construct(MusicRepository $model)
    {
        parent::__construct();
        $this->model = $model;
    }

    /**
     * 获取默认歌单详情
     *
     * @return \App\Http\Resources\Item|array|object
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     *
     * @SWG\Get(path="/music/profile",
     *     tags={"Music"},
     *     summary="获取默认歌单详情",
     *     description="获取默认歌单详情",
     *     security={{"api_key": {}}},
     *     deprecated=false,
     *     @SWG\Response(response=200,description="success",
     *          @SWG\Schema(ref="#/definitions/songMenuDefault")
     *     ),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401,description="未登录")
     * )
     */
    public function menuProfile()
    {
        $menuModel = app()->make(SongMenuRepository::class);
        if (!$model = $menuModel->findDefaultMenu()) {
            return [];
        }
        return $this->resourceInstance(SongMenu::class, $model)->setStamp('player');
    }

    /**
     * 获取歌曲播放信息
     *
     * @param $id
     * @return \App\Http\Resources\Item|array|object
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     *
     * @SWG\Get(path="/music/song/{id}",
     *     tags={"Music"},
     *     description="获取歌曲播放信息",
     *     produces={"application/json"},
     *     @SWG\Parameter(in="path", name="id", type="string", description="歌曲id", required=true),
     *     @SWG\Response(response=200, description="success"),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401, description="未登录")
     * )
     */
    public function song($id)
    {
        $songModel = app()->make(SongRepository::class);
        $song = $songModel->findBySongId($id);

        return $this->resourceInstance(Song::class, $song)
            ->setStamp('info');
    }

    /**
     * @param Request $request
     * @return mixed|\Psr\Http\Message\ResponseInterface|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     *
     * @SWG\Get(path="/music/search",
     *     tags={"Music"},
     *     description="搜索歌曲列表",
     *     produces={"application/json"},
     *     @SWG\Parameter(in="query", name="q", type="string", description="搜索内容 按标题", required=true),
     *     @SWG\Parameter(ref="#/parameters/offsetPageParam"),
     *     @SWG\Parameter(ref="#/parameters/offsetLimitParam"),
     *     @SWG\Response(response=200, description="success"),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401, description="未登录")
     * )
     */
    public function search(Request $request)
    {
        return [
            'data' => $this->model->search($request),
            'meta' => $this->model->getMeta()
        ];
    }
}
